package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Movement;
import com.itheima.service.MovementService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ${小帅帅}
 * @date 2019-11-04
 * 运动知识库
 */
@RestController
@RequestMapping("/movement")
public class MovementController {

    @Reference
    private MovementService movementService;



    //运动知识库文件上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {

        try {

            //使用POI的工具类,读取并解析文件，
            List<String[]> list = POIUtils.readExcel(excelFile);

            //创建一个集合，存储处理当前的list结构
            List<Movement> movementList = new ArrayList<>();

            //处理list，变成有利于数据库存储的格式
            for (String[] strings : list) {

                //获取主键ID，表格中，ID的索引为0
                String id1 = strings[0];
                Integer id = Integer.parseInt(id1);
                //获取运动编码。1
                String YDCode = strings[1];
                //获取运动名称。2
                String name = strings[2];
                //获取运动频率/min。3
                String time = strings[3];
                //获取每周运动次数。4
                String frequency = strings[4];
                //获取运动种类。5
                String type1 = strings[5];
                int type = Integer.parseInt(type1);
                //获取运动强度。6
                String intensity1 = strings[6];
                int intensity = Integer.parseInt(intensity1);
                //获取最小年龄.7
                String minAge = strings[7];
                //获取最大年龄
                String maxAge = strings[8];
                //获取卡路里
                String kll = strings[9];

                Movement movement = new Movement(id, YDCode, name, time, frequency, type, intensity, minAge, maxAge, kll);
                //保存到list集合
                movementList.add(movement);
            }

            //调用导入病因知识库的方法
            movementService.movementAdd(movementList);

            //文件解析成功，给出提示信息
            return new Result(true, MessageConstant.SELECT_MEM_SUCCESS);

        } catch (Exception e) {

            e.printStackTrace();
            //文件解析失败，给出提示信息
            return new Result(false, MessageConstant.SELECT_MEM_FAIL);
        }
    }


    //运动知识库，分页查询展示
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean) throws Exception {

        //运动知识库分页查询的调用
        PageResult page = movementService.findPage(pageBean);
        return page;
    }

}
